package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class LoginFilter extends HttpServlet implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse res=(HttpServletResponse)response;
		
		
		String contextPath=req.getContextPath();
		String url=req.getServletPath();

		HttpSession  session=req.getSession();
		
		System.out.println("upgrade:"+req.getHeader("Upgrade"));
		System.out.println("url:"+url);
		if(!url.startsWith("/index")&&req.getHeader("Upgrade")==null){
			if(session.getAttribute("username")==null){
				System.out.println("username:"+session.getAttribute("username"));
				res.sendRedirect(contextPath+"/index.jsp");
				return ;
			}
		}
		chain.doFilter(request, response);
	}

	

}
